﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Xml.Linq;

namespace EzDesk.Generic
{
    public class XmlHelper
    {
        public static string TranslateXml(string xsltUri, string xmlData,string namespaceUri, params KeyValuePair<string, string>[] keyValues)
        {
            XslCompiledTransform xslTransform = new XslCompiledTransform();
            xslTransform.Load(xsltUri);
            return TranslateXml(xslTransform, xmlData, namespaceUri,keyValues);
        }

        /// @refactoring
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xslTransform"></param>
        /// <param name="xmlUrl"></param>
        /// <param name="namespaceUri"></param>
        /// <param name="keyValues"></param>
        /// <returns></returns>
        public static string TranslateXml(XslCompiledTransform xslTransform, string xmlUri, string namespaceUri, params KeyValuePair<string, string>[] keyValues)
        {
            XPathDocument xpathDocument = new XPathDocument(xmlUri);
            XmlReader xr = XmlReader.Create(xmlUri);
            XsltArgumentList argumentList = new XsltArgumentList();
            keyValues = keyValues ?? new KeyValuePair<string, string>[] { };
            foreach (var pair in keyValues)
            {
                argumentList.AddParam(pair.Key, namespaceUri, pair.Value);
            }
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            xslTransform.Transform(xr, argumentList, sw);
            return sb.ToString();
        }
    }
}
